package com.knowif.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.knowif.dao.UserCommentMapper;
import com.knowif.dao.UserHelpfulMapper;
import com.knowif.enumInfo.InfoStatusEnum;
import com.knowif.pojo.UserComment;
import com.knowif.pojo.UserCommentExample;
import com.knowif.pojo.UserHelpful;
import com.knowif.pojo.UserHelpfulExample;
import com.knowif.system.model.RestBean;

/**
 * @author ：Xh
 * @date ：Created in 2019/5/4 0004 21:28
 */
@Service
public class UserHelpful_service {

    @Autowired
    private UserHelpfulMapper userHelpfulMapper;
    
    
	@Autowired
	private UserCommentMapper userCommentMapper;

    /**
     * UserHelpful条件查询
     * @param userHelpfulExample
     * @return
     */
    public List<UserHelpful> selectUserHelpful(UserHelpfulExample userHelpfulExample){
        return userHelpfulMapper.selectByExample(userHelpfulExample);
    }
    
    /**
     * @author: xingyao 
     * @Description: 通过评论ids得到所有的用户评论树
     * @param commentIds
     * @return
     */
    public List<UserHelpful> selectByCommentIds(List<Integer> commentIds){
    	if(commentIds == null || commentIds.isEmpty()){
    		return new ArrayList<UserHelpful>();
    	}
    	UserHelpfulExample userHelpfulExample = new UserHelpfulExample();
    	userHelpfulExample.createCriteria().andUserCommentIdIn(commentIds);
    	return userHelpfulMapper.selectByExample(userHelpfulExample);
    }
    

    /**
     * 插入userHelpful
     * @param userHelpful
     * @return
     */
    private RestBean<?> saveUserHelpful(UserHelpful userHelpful){
    	if(userHelpful == null){
			return RestBean.buildError("请填写需要点赞的信息");
		}
		//保存有用信息
		userHelpful.setCreateTime(new Date());
		userHelpfulMapper.insert(userHelpful);
		//将评论里面的userHelp_num+1
		UserComment userComment = userCommentMapper.selectByPrimaryKey(userHelpful.getUserCommentId());
		userComment.setHelpfulCount(userComment.getHelpfulCount()+1);
		UserCommentExample userCommentExample = new UserCommentExample();
		userCommentExample.createCriteria().andIdEqualTo(userHelpful.getUserCommentId());
		userCommentMapper.updateByExample(userComment, userCommentExample);
		return RestBean.buildSuccess(InfoStatusEnum.ISUSED.getStatus());
    }
    
    
    @Transactional
    private RestBean<?> deleteUserHelpful(UserHelpful userHelpful){
    	if(userHelpful == null){
			return RestBean.buildError("请填写删除点赞的信息的信息");
		}
    	//将评论里面的userHelp_num-1
		UserComment userComment = userCommentMapper.selectByPrimaryKey(userHelpful.getUserCommentId());
		userComment.setHelpfulCount(userComment.getHelpfulCount()+1);
		UserCommentExample userCommentExample = new UserCommentExample();
		userCommentExample.createCriteria().andIdEqualTo(userHelpful.getUserCommentId());
		userCommentMapper.updateByExample(userComment, userCommentExample);
		userHelpfulMapper.deleteByPrimaryKey(userHelpful.getId());
		return RestBean.buildSuccess(InfoStatusEnum.ISNOTUSED.getStatus());
    }
    
    
//    public UserHelpful selectBycommendIdAndUserId()
    
    
    /**
     * @author: xingyao 
     * @Description:  给评论点赞
     * @param userHelpful
     * @return
     */
    public RestBean<?> isUsed(UserHelpful userHelpful){
    	//如果存在该用户给这个评论点赞，则功能为取消点赞
    	UserHelpfulExample userHelpfulExample = new UserHelpfulExample();
    	userHelpfulExample.createCriteria()
    			.andUserCommentIdEqualTo(userHelpful.getUserCommentId())
    			.andUserIdEqualTo(userHelpful.getUserId());
    	List<UserHelpful> userHelpfuls = userHelpfulMapper.selectByExample(userHelpfulExample);
    	if(userHelpfuls == null || userHelpfuls.isEmpty()){
    		//进行点赞功能
    		return saveUserHelpful(userHelpful);
    	}else{
    		//取消点赞功能
    		return deleteUserHelpful(userHelpful);
    	}
    }

    /**
     * 更新不为空的userHelpful属性
     * @param userHelpful
     * @return
     */
    public int updateUserHelpfulNotNull(UserHelpful userHelpful){
        return userHelpfulMapper.updateByPrimaryKeySelective(userHelpful);
    }

    /**
     * 更新所有userHelpful属性
     * @param userHelpful
     * @return
     */
    public int updateUserHelpful(UserHelpful userHelpful){
        return userHelpfulMapper.updateByPrimaryKey(userHelpful);
    }

    /**
     * 删除userHelpful
     * @param userHelpfulId
     */
    public int deleteUserHelpful(int userHelpfulId){
        return userHelpfulMapper.deleteByPrimaryKey(userHelpfulId);
    }
}
